package cn.aic.pointInspect.sysquery;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import cn.aic.pointInspect.tools.MyBatisUtil;
import cn.aic.pointInspect.tools.Util;

/**
 * @author          liufei
 *
 * @Date            2018年7月11日-下午4:13:40
 *
 * @Descirption     组织机构管理
 * 
 */
public class DataProviderImpl implements DataProvider{
	SqlSession openSession = MyBatisUtil.getSession();
	/**查询巡检系统的组织机构
	 * @param guid 
	 * @return
	 */
	public Map queryXJOrgan(String guid) {
		Map<String,Object> returnMap = new HashMap<String,Object>();
		
		List organList = openSession.selectList("xjOrgan.queryXJOrgan","");
		returnMap.put("organList", organList);
		returnMap.put("success", true);
		return returnMap;
	}
	
	/**查询巡检系统的线路通过组织机构
	 * @param guid 
	 * @return
	 */
	public Map queryXJLineByOrgan(String t_organization_r_guid) {
		Map<String,Object> returnMap = new HashMap<String,Object>();
//		if(Util.isNullOrEmpty(t_organization_r_guid)){
//			returnMap.put("success", false);
//			returnMap.put("errorInfo", "Parameter is null;");
//			return returnMap;
//		}
		Map<String,Object> dataMap =new HashMap<String,Object>();
		dataMap.put("t_organization_r_guid", t_organization_r_guid);
		dataMap.put("date", new Date());
		
		List lineList = openSession.selectList("xjOrgan.queryXJLineByOrgan",dataMap);
		returnMap.put("lineList", lineList);
		returnMap.put("success", true);
		return returnMap;
	}
	
	/**查询巡检系统的设备通过线路
	 * @param guid 
	 * @return
	 */
	public Map queryXJDeviceByLine(String t_organization_r_guid,String t_line_r_guid) {
		Map<String,Object> returnMap = new HashMap<String,Object>();
//		if(Util.isNullOrEmpty(t_organization_r_guid) || Util.isNullOrEmpty(t_line_r_guid)){
//			returnMap.put("success", false);
//			returnMap.put("errorInfo", "Parameter is null;");
//			return returnMap;
//		}
		Map<String,Object> dataMap =new HashMap<String,Object>();
		dataMap.put("t_organization_r_guid", t_organization_r_guid);
		dataMap.put("t_line_r_guid", t_line_r_guid);
		
		List deviceList = openSession.selectList("xjOrgan.queryXJDeviceByLine",dataMap);
		returnMap.put("deviceList", deviceList);
		returnMap.put("success", true);
		return returnMap;
	}
	/**查询巡检系统的检测项通过设备
	 * @param guid 
	 * @return
	 */
	public Map<String,Object> queryXJItemByDevice(String t_organization_r_guid,String t_line_r_guid,String t_device_r_guid) {
		Map<String,Object> returnMap = new HashMap<String,Object>();
//		if(Util.isNullOrEmpty(t_organization_r_guid) || Util.isNullOrEmpty(t_line_r_guid) || Util.isNullOrEmpty(t_device_r_guid)){
//			returnMap.put("success", false);
//			returnMap.put("errorInfo", "Parameter is null;");
//			return returnMap;
//		}
		Map<String,Object> dataMap =new HashMap<String,Object>();
		dataMap.put("t_organization_r_guid", t_organization_r_guid);
		dataMap.put("t_line_r_guid", t_line_r_guid);
		dataMap.put("t_device_r_guid", t_device_r_guid);
		
		List deviceList = openSession.selectList("xjOrgan.queryXJItemByDevice",dataMap);
		returnMap.put("itemList", deviceList);
		returnMap.put("success", true);
		return returnMap;
	}

	@Override
	public Map queryAllDataOfXJ(String alias_name,String t_organization_r_guid,
			String t_line_r_guid, String t_device_r_guid, String t_item_r_guid,Date beginDate,Date endDate,
			String is_special_inspection,String item_define,String is_normal,
			String is_in_place,String grade,String devFullName,String t_item_abnormal_grade_guid
			,String t_measure_type_guid) {
		
		List returnDataList  = new ArrayList();
		if(Util.isNullOrEmpty(beginDate)){
			endDate = new Date();
			beginDate = (java.util.Date)Util.rollDateByWeek(endDate, 1, true);
		}
		
		Map<String,Object> dataMap = new HashMap<String,Object>();
		dataMap.put("t_organization_r_guid",t_organization_r_guid );
		dataMap.put("t_line_r_guid", t_line_r_guid);
		dataMap.put("t_device_r_guid",t_device_r_guid);
		dataMap.put("t_item_r_guid", t_item_r_guid);
		dataMap.put("beginDate", beginDate);
		dataMap.put("endDate", endDate);
		dataMap.put("is_special_inspection", is_special_inspection);
		dataMap.put("item_define", item_define);
		dataMap.put("is_normal", is_normal);
		dataMap.put("is_in_place", is_in_place);
		dataMap.put("grade", grade);
		dataMap.put("devFullName", devFullName);
		dataMap.put("t_item_abnormal_grade_guid", t_item_abnormal_grade_guid);
		dataMap.put("alias_name", alias_name);
		dataMap.put("t_measure_type_guid", t_measure_type_guid);
		Calendar beginCalendar = Calendar.getInstance();
		Calendar endCalendar = Calendar.getInstance();
		beginCalendar.setTime(beginDate);
		endCalendar.setTime(endDate);
		
		int beginYear = beginCalendar.get(Calendar.YEAR);
		int endYear = endCalendar.get(Calendar.YEAR);
		int beginMonth = beginCalendar.get(Calendar.MONTH)+1;
		int endMonth = endCalendar.get(Calendar.MONTH)+1;
		for (int i = beginYear; i <= endYear; i++) {//按年份遍历查询
			if(i == endYear){//同年，则执行开始月到结束月
				for (int j = beginMonth; j <= endMonth; j++) {
					if(j<10){
						dataMap.put("table", "t_item_r_"+i+"0"+j);
					}else{
						dataMap.put("table", "t_item_r_"+i+j);
					}
					List oneDataList = null;
					try {
						oneDataList = openSession.selectList("xjDataQuery.queryXJAllData",dataMap);
					} catch (Exception e) {
						System.out.println("===================查询数据库返回的错误结果：========================"+ e.getMessage());
					}
					if(!Util.isNullOrEmpty(oneDataList)){
						returnDataList.addAll(oneDataList);
					}
				}
			}else if(i != endYear){//不同年，则执行开始月到12月
				for (int j = beginMonth; j <= 12; j++) {
					if(j<10){
						dataMap.put("table", "t_item_r_"+i+"0"+j);
					}else{
						dataMap.put("table", "t_item_r_"+i+j);
					}
					List oneDataList = null;
					try {
						oneDataList = openSession.selectList("xjDataQuery.queryXJAllData",dataMap);
					} catch (Exception e) {
						System.out.println("===================查询数据库返回的错误结果：========================"+ e.getMessage());
					}
					if(!Util.isNullOrEmpty(oneDataList)){
						returnDataList.addAll(oneDataList);
					}
				}
				beginMonth = 1;
			}
		}
		
		Map<String,Object> returnMap = new HashMap<String,Object>();
		returnMap.put("returnDataList", returnDataList);
		return returnMap;
	}

	/* 查询漏检数据
	 * beginDate: 漏检开始时间
	 * endDate：   漏检结束时间
	 */
	@Override
	public Map queryLeakChectDataOfXJ(Date beginDate, Date endDate,String turnGuid,String lineGuid,Boolean isDeviceChecked,String t_organization_r_name) {
		
		//查询漏检数据
		Map<String,Object> dataMap = new HashMap<String,Object>();
		dataMap.put("beginDate",beginDate);
		dataMap.put("endDate", endDate);
		dataMap.put("turnGuid", turnGuid);
		dataMap.put("lineGuid", lineGuid);
		dataMap.put("isDeviceChecked", isDeviceChecked);
		dataMap.put("t_organization_r_name", t_organization_r_name);
		
		Calendar beginCalendar = Calendar.getInstance();
		Calendar endCalendar = Calendar.getInstance();
		beginCalendar.setTime(beginDate);
		endCalendar.setTime(endDate);
		
		
		List returnDataList  = new ArrayList();
		int beginYear = beginCalendar.get(Calendar.YEAR);
		int endYear = endCalendar.get(Calendar.YEAR);
		int beginMonth = beginCalendar.get(Calendar.MONTH)+1;
		int endMonth = endCalendar.get(Calendar.MONTH)+1;
		
		for (int i = beginYear; i <= endYear; i++) {//按年份遍历查询
			if(i == endYear){//同年，则执行开始月到结束月
				for (int j = beginMonth; j <= endMonth; j++) {
					if(j<10){
						dataMap.put("table", "t_device_r_"+i+"0"+j);
					}else{
						dataMap.put("table", "t_device_r_"+i+j);
					}
					List oneDataList = null;
					try {
						oneDataList = openSession.selectList("xjDataQuery.queryLeakChectData",dataMap);
					} catch (Exception e) {
						System.out.println("===================查询数据库返回的错误结果：========================"+ e.getMessage());
					}
					if(!Util.isNullOrEmpty(oneDataList)){
						returnDataList.addAll(oneDataList);
					}
				}
			}else if(i != endYear){//不同年，则执行开始月到12月
				for (int j = beginMonth; j <= 12; j++) {
					if(j<10){
						dataMap.put("table", "t_device_r_"+i+"0"+j);
					}else{
						dataMap.put("table", "t_device_r_"+i+j);
					}
					List oneDataList = null;
					try {
						oneDataList = openSession.selectList("xjDataQuery.queryLeakChectData",dataMap);
					} catch (Exception e) {
						System.out.println("===================查询数据库返回的错误结果：========================"+ e.getMessage());
					}
					if(!Util.isNullOrEmpty(oneDataList)){//数目求和
						returnDataList.addAll(oneDataList);
					}
				}
				beginMonth = 1;
			}
		}
		
		Map<String,Object> returnMap = new HashMap<String,Object>();
		returnMap.put("returnDataList", returnDataList);
		returnMap.put("success", true);
		return returnMap;
	}

	@Override
	public Map queryLeakChectCountOfXJ(Date beginDate, Date endDate,String turnGuid,String lineGuid,String isDeviceChecked,
			String t_organization_r_name) {
		//查询漏检数据
				Map<String,Object> dataMap = new HashMap<String,Object>();
				dataMap.put("t_organization_r_name", t_organization_r_name);
				
				Calendar beginCalendar = Calendar.getInstance();
				Calendar endCalendar = Calendar.getInstance();
				beginCalendar.setTime(beginDate);
				endCalendar.setTime(endDate);
				dataMap.put("beginDate",beginDate);
				dataMap.put("endDate", endDate);
				dataMap.put("turnGuid",turnGuid);
				dataMap.put("lineGuid", lineGuid);
				dataMap.put("isDeviceChecked", isDeviceChecked);
				
				List returnDataList  = new ArrayList();
				int beginYear = beginCalendar.get(Calendar.YEAR);
				int endYear = endCalendar.get(Calendar.YEAR);
				int beginMonth = beginCalendar.get(Calendar.MONTH)+1;
				int endMonth = endCalendar.get(Calendar.MONTH)+1;
				for (int year = beginYear; year <= endYear; year++) {//按年份遍历查询
					if(year == endYear){//同年，则执行开始月到结束月
						for (int mouth = beginMonth; mouth <= endMonth; mouth++) {
							if(mouth<10){
								dataMap.put("table", "t_device_r_"+year+"0"+mouth);
							}else{
								dataMap.put("table", "t_device_r_"+year+mouth);
							}
							List oneDataList = null;
							try {
								oneDataList = openSession.selectList("xjDataQuery.queryLeakChectCount",dataMap);
							} catch (Exception e) {
								System.out.println("===================查询数据库返回的错误结果：========================"+ e.getMessage());
							}
							if(returnDataList.size() <= 0 && !Util.isNullOrEmpty(oneDataList)){
								returnDataList.addAll(oneDataList);
							}else{
								if(!Util.isNullOrEmpty(oneDataList)){
									for (int i = 0; i < oneDataList.size(); i++) {//遍历子数据
										Map leakChectMap = (Map) oneDataList.get(i);
											for (int j = 0; j < returnDataList.size(); j++) {//遍历统计数据，求sum和统计
												Map leakChectStaMap = (Map) returnDataList.get(j);
												if(leakChectMap.get("t_organization_r_guid").equals(leakChectStaMap.get("t_organization_r_guid"))){
													int divNum = (int) leakChectMap.get("sum");
													int staNum = (int) leakChectStaMap.get("sum");
													leakChectStaMap.put("sum", divNum+staNum);
													returnDataList.set(j, leakChectStaMap);
													break;
												}
											}
									}
								}
							}
						}
					}else if(year != endYear){//不同年，则执行开始月到12月
						for (int mouth = beginMonth; mouth <= 12; mouth++) {
							if(mouth<10){
								dataMap.put("table", "t_device_r_"+year+"0"+mouth);
							}else{
								dataMap.put("table", "t_device_r_"+year+mouth);
							}
							List oneDataList = null;
							try {
								oneDataList = openSession.selectList("xjDataQuery.queryLeakChectCount",dataMap);
							} catch (Exception e) {
								System.out.println("===================查询数据库返回的错误结果：========================"+ e.getMessage());
							}
							if(returnDataList.size() <= 0 && !Util.isNullOrEmpty(oneDataList)){
								returnDataList.addAll(oneDataList);
							}else{
								if(!Util.isNullOrEmpty(oneDataList)){
									for (int i = 0; i < oneDataList.size(); i++) {
										Map leakChectMap = (Map) oneDataList.get(i);
											for (int j = 0; j < returnDataList.size(); j++) {
												Map leakChectStaMap = (Map) returnDataList.get(j);
												if(leakChectMap.get("t_organization_r_guid").equals(leakChectStaMap.get("t_organization_r_guid"))){
													int divNum = (int) leakChectMap.get("sum");
													int staNum = (int) leakChectStaMap.get("sum");
													leakChectStaMap.put("sum", divNum+staNum);
													returnDataList.set(j, leakChectStaMap);
												}
											}
									}
								}
							}
						}
						beginMonth = 1;
					}
				}
				Map<String,Object> returnMap = new HashMap<String,Object>();
				returnMap.put("returnDataList", returnDataList);
				returnMap.put("success", true);
				return returnMap;
	}

	/* 查询异常数据
	 * 
	 */
	@Override
	public Map queryAbnormalCountOfXJ(Date beginDate, Date endDate,
			String t_organization_r_name) {
				
		//查询漏检数据
		Map<String,Object> dataMap = new HashMap<String,Object>();
		dataMap.put("beginDate",beginDate );
		dataMap.put("endDate", endDate);
		dataMap.put("t_organization_r_name", t_organization_r_name);
		
		Calendar beginCalendar = Calendar.getInstance();
		Calendar endCalendar = Calendar.getInstance();
		beginCalendar.setTime(beginDate);
		endCalendar.setTime(endDate);
		
		
		List returnDataList  = new ArrayList();
		returnDataList = openSession.selectList("xjDataQuery.queryAbnormalDataCount",dataMap);
		Map<String,Object> returnMap = new HashMap<String,Object>();
		returnMap.put("returnDataList", returnDataList);
		returnMap.put("success", true);
		return returnMap;
	}
	
	/* 查询漏检数据
	 * beginDate: 漏检开始时间
	 * endDate：   漏检结束时间
	 */
	@Override
	public Map queryAbnormalDataOfXJ(Date beginDate, Date endDate,String t_organization_r_name) {
		
		//查询漏检数据
		Map<String,Object> dataMap = new HashMap<String,Object>();
		dataMap.put("beginDate",beginDate );
		dataMap.put("endDate", endDate);
		dataMap.put("t_organization_r_name", t_organization_r_name);
		
		Calendar beginCalendar = Calendar.getInstance();
		Calendar endCalendar = Calendar.getInstance();
		beginCalendar.setTime(beginDate);
		endCalendar.setTime(endDate);
		
		List returnDataList  = new ArrayList();
		returnDataList = openSession.selectList("xjDataQuery.queryAbnormalData",dataMap);
		
		Map<String,Object> returnMap = new HashMap<String,Object>();
		returnMap.put("returnDataList", returnDataList);
		returnMap.put("success", true);
		return returnMap;
	}

	/* 查询缺陷未处理数据
	 * 
	 */
	@Override
	public Map queryFlawCountOfXJ(Date beginDate, Date endDate,
			String t_organization_r_name) {
		//查询漏检数据
//		beginDate = Util.getString2Date("2018-06-01");
//		endDate = Util.getString2Date("2018-06-30");
		Map<String,Object> dataMap = new HashMap<String,Object>();
		dataMap.put("beginDate",beginDate );
		dataMap.put("endDate", endDate);
		dataMap.put("t_organization_r_name", t_organization_r_name);
		
		Calendar beginCalendar = Calendar.getInstance();
		Calendar endCalendar = Calendar.getInstance();
		beginCalendar.setTime(beginDate);
		endCalendar.setTime(endDate);
		
		
		
		//查询未处理的 flawType is null
		dataMap.put("flawType", "notdo");
		List flawNotDoDataCountList = openSession.selectList("xjDataQuery.queryFlawCount",dataMap);
		
		//查询正处理的 flawType = 0
		dataMap.put("flawType", "doing");
		List flawDoingDataCountList = openSession.selectList("xjDataQuery.queryFlawCount",dataMap);
		
		//查询已处理的 flawType = 1
		dataMap.put("flawType", "done");
		List flawDoneDataCountList = openSession.selectList("xjDataQuery.queryFlawCount",dataMap);
		
		
		Map<String,Object> returnMap = new HashMap<String,Object>();
		returnMap.put("flawNotDoDataCountList", flawNotDoDataCountList);
		returnMap.put("flawDoingDataCountList", flawDoingDataCountList);
		returnMap.put("flawDoneDataCountList", flawDoneDataCountList);
		returnMap.put("success", true);
		return returnMap;
	}

	@Override
	public Map queryFlawDataOfXJ(Date beginDate, Date endDate,
			String t_organization_r_name,String flawType) {
		
			//查询漏检数据
			Map<String,Object> dataMap = new HashMap<String,Object>();
			dataMap.put("beginDate",beginDate );
			dataMap.put("endDate", endDate);
			dataMap.put("t_organization_r_name", t_organization_r_name);
			switch (flawType) {
			case "未处理":
				dataMap.put("flawType", "notdo");
				break;
			case "处理中":
				dataMap.put("flawType", "doing");
				break;
			case "已处理":
				dataMap.put("flawType", "done");
				break;
			}
			
			List returnDataList  = new ArrayList();
			returnDataList = openSession.selectList("xjDataQuery.queryAbnormalData",dataMap);
			
			Map<String,Object> returnMap = new HashMap<String,Object>();
			returnMap.put("returnDataList", returnDataList);
			returnMap.put("success", true);
			return returnMap;
	}

	@Override
	public Map queryAllStaffDataOfXJ(String t_organization_r_guid) {
		Map<String,Object> dataMap = new HashMap<String,Object>();
		dataMap.put("t_organization_r_guid", t_organization_r_guid);
		List returnDataList  = new ArrayList();
		returnDataList = openSession.selectList("xjDataQuery.queryAllStaffData",dataMap);
		Map<String,Object> returnMap = new HashMap<String,Object>();
		returnMap.put("returnDataList", returnDataList);
		returnMap.put("success", true);
		return returnMap;
	}

	@Override
	public Map queryAllAbnormalGradeDataOfXJ() {
		List abnormalGradeDataList = openSession.selectList("xjDataQuery.queryAllAbnormalGradeData",null);
		Map<String,Object> returnMap = new HashMap<String,Object>();
		returnMap.put("abnormalGradeDataList", abnormalGradeDataList);
		returnMap.put("success", true);
		return returnMap;
	}

	@Override
	public Map queryTurnByLineInfoOfXJ(String t_line_r_guid,
			String t_line_r_content_guid) {
		Map<String,Object> dataMap = new HashMap<String,Object>();
		dataMap.put("t_line_r_guid", t_line_r_guid);
		dataMap.put("t_line_r_content_guid", t_line_r_content_guid);
		List returnDataList  = new ArrayList();
		returnDataList = openSession.selectList("xjDataQuery.queryTurnByLineData",dataMap);
		Map<String,Object> returnMap = new HashMap<String,Object>();
		returnMap.put("returnDataList", returnDataList);
		returnMap.put("success", true);
		return returnMap;
	}
	
	
	/**查询巡检系统的测量类型
	 * @param t_measure_type_guid 
	 * @return
	 */
	public Map<String,Object> queryMeasureType(String t_measure_type_guid) {
		Map<String,Object> returnMap = new HashMap<String,Object>();
		Map<String,Object> dataMap =new HashMap<String,Object>();
		dataMap.put("t_measure_type_guid", t_measure_type_guid);
		
		List returnDataList = openSession.selectList("xjDataQuery.queryMeasureType",dataMap);
		returnMap.put("returnDataList", returnDataList);
		returnMap.put("success", true);
		return returnMap;
	}
}
